package study.alex.resumedb.gae.model;

import org.datanucleus.api.jpa.annotations.Extension;
import study.alex.resumedb.gae.ResumeBlob;
import study.alex.resumedb.gae.ResumeBlobException;
import javax.persistence.*;
import java.io.IOException;
import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 24.01.14
 * Time: 17:05
 * SVN $Id$
 */
@Entity
@MappedSuperclass
public class AbstractResumeBlob implements ResumeBlob, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    protected String id;

    protected String userId = null;

    @OneToMany (fetch = FetchType.EAGER, cascade = {CascadeType.MERGE}, mappedBy = "body")
    protected Resume resume;

    @Override
    public boolean storeBlob(byte[] body, String mimeType) throws ResumeBlobException, IOException {
        return storeBlob(body, mimeType, createRandomName("res"));
    }

    private String createRandomName(String prefix) {

        StringBuilder name = new StringBuilder(prefix);

        GregorianCalendar today = new GregorianCalendar();
        String suffix = "-" + new Random().nextInt(1000);

        name.append(today.toString());
        name.append(suffix);
        return name.toString();
    }

    @Override
    public boolean storeBlob(byte[] body, String mimeType, String name) throws ResumeBlobException, IOException {
        return false;
    }

    @Override
    public byte[] getBlob() throws ResumeBlobException, IOException {
        return new byte[0];
    }

    @Override
    public String getType() {
        return null;
    }

    @Override
    public boolean deleteBlob() throws ResumeBlobException, IOException {
        return false;
    }

    @Override
    public long getMaxSize() throws ResumeBlobException, IOException {
        return 0;
    }

    @Override
    public long getSize() throws ResumeBlobException, IOException {
        return 0;
    }

    @Override
    public long getMaxSize(String type) throws ResumeBlobException, IOException {
        return 0;
    }

    public AbstractResumeBlob(String userId, Resume resume) {
        this.userId = userId;
        this.resume = resume;
    }

    @Override
    public String getId() {
        return id;
    }

    public String getUserId() {
        return userId;
    }

    @Override
    public String getPreviewURL() {
        return null;
    }

    @Override
    public String getOpenURL() throws ResumeBlobException, IOException {
        return null;
    }

    @Override
    public String getThumbnailLink() throws ResumeBlobException, IOException {
        return null;
    }
}
